import 'package:dio/dio.dart';
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart';

void main() async {
  print("start");
  await Spider.getBookDetails("https://www.biquge.com.cn/");
  print("stop");
}

String baseUrl = "https://www.biquge.com.cn";

class Spider {
  static getBookDetails(String bookUrl) async {
    Response resp = await Dio().get(bookUrl);
    String data = resp.data;
    // 解析页面元素
    Document documnet = parse(data);
    // 解析book相关信息
    List<Element> bookTypes = documnet.querySelectorAll(".novelslist");
    List<Element> bookFirstRow = bookTypes[0].querySelectorAll(".content");
    bookFirstRow.addAll(bookTypes[1].querySelectorAll(".content"));
    List result = [];
    bookFirstRow.forEach((element) { 
      String bookType =  element.getElementsByTagName("h2")[0].innerHtml;
      Element topBook = element.getElementsByClassName("top")[0];
      Map topContent = {
        "imgUrl": topBook.getElementsByTagName("img")[0].attributes["src"],
        "bookUrl": baseUrl + topBook.getElementsByTagName("a")[0].attributes["href"],
        "title": topBook.getElementsByTagName("dt")[0].getElementsByTagName("a")[0].innerHtml,
        "description": topBook.getElementsByTagName("dd")[0].innerHtml
      };
      List<Element> otherBooks = element.getElementsByTagName("li");
      List other = otherBooks.map((e){
        return {
          "title": e.getElementsByTagName("a")[0].innerHtml,
          "author": e.innerHtml.split(r"/").removeLast(),
          "bookUrl": baseUrl + e.getElementsByTagName("a")[0].attributes["href"]
        };
      }).toList();  

      Map tempResult = {
        "bookType": bookType,
        "topBook": topContent,
        "otherBooks": other
      };
      result.add(tempResult);
    });
    return result;
  }
}
